﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;

namespace Eneta.Portal.Forum.Rss
{
    public class RssReader
    {
    // Fields
    private RssCategory category;
    private RssChannel channel;
    private RssCloud cloud;
    private StringBuilder elementText;
    private RssEnclosure enclosure;
    private ExceptionCollection exceptions;
    private RssGuid guid;
    private RssImage image;
    private RssItem item;
    private XmlTextReader reader;
    private RssVersion rssVersion;
    private RssSource source;
    private RssTextInput textInput;
    private bool wroteChannel;
    private Stack xmlNodeStack;

    // Methods
    public RssReader(Stream stream)
    {
        this.xmlNodeStack = new Stack();
        this.elementText = new StringBuilder();
        this.reader = null;
        this.wroteChannel = false;
        this.rssVersion = RssVersion.Empty;
        this.exceptions = new ExceptionCollection();
        this.textInput = null;
        this.image = null;
        this.cloud = null;
        this.channel = null;
        this.source = null;
        this.enclosure = null;
        this.guid = null;
        this.category = null;
        this.item = null;
        try
        {
            this.reader = new XmlTextReader(stream);
            this.InitReader();
        }
        catch (Exception exception)
        {
            throw new ArgumentException("Unable to retrieve file containing the RSS data.", exception);
        }
    }

    public RssReader(TextReader textReader)
    {
        this.xmlNodeStack = new Stack();
        this.elementText = new StringBuilder();
        this.reader = null;
        this.wroteChannel = false;
        this.rssVersion = RssVersion.Empty;
        this.exceptions = new ExceptionCollection();
        this.textInput = null;
        this.image = null;
        this.cloud = null;
        this.channel = null;
        this.source = null;
        this.enclosure = null;
        this.guid = null;
        this.category = null;
        this.item = null;
        try
        {
            this.reader = new XmlTextReader(textReader);
            this.InitReader();
        }
        catch (Exception exception)
        {
            throw new ArgumentException("Unable to retrieve file containing the RSS data.", exception);
        }
    }

    public RssReader(string url)
    {
        this.xmlNodeStack = new Stack();
        this.elementText = new StringBuilder();
        this.reader = null;
        this.wroteChannel = false;
        this.rssVersion = RssVersion.Empty;
        this.exceptions = new ExceptionCollection();
        this.textInput = null;
        this.image = null;
        this.cloud = null;
        this.channel = null;
        this.source = null;
        this.enclosure = null;
        this.guid = null;
        this.category = null;
        this.item = null;
        try
        {
            this.reader = new XmlTextReader(url);
            this.InitReader();
        }
        catch (Exception exception)
        {
            throw new ArgumentException("Unable to retrieve file containing the RSS data.", exception);
        }
    }

    public void Close()
    {
        this.textInput = null;
        image = null;
        cloud = null;
        channel = null;
        this.source = null;
        this.enclosure = null;
        this.category = null;
        this.item = null;
        if (this.reader != null)
        {
            this.reader.Close();
            this.reader = null;
        }
        this.elementText = null;
        this.xmlNodeStack = null;
    }

    private void InitReader()
    {
        reader.WhitespaceHandling = (WhitespaceHandling)2;
        reader.XmlResolver = null;
    }

    public RssElement Read()
    {
        string str;
        /*if (<PrivateImplementationDetails>.$$method0x6000282-1 == null)
        {
            Hashtable hashtable1 = new Hashtable(0x1a, 0.5f);
            hashtable1.Add("title", 0);
            hashtable1.Add("link", 1);
            hashtable1.Add("description", 2);
            hashtable1.Add("language", 3);
            hashtable1.Add("copyright", 4);
            hashtable1.Add("managingeditor", 5);
            hashtable1.Add("webmaster", 6);
            hashtable1.Add("rating", 7);
            hashtable1.Add("pubdate", 8);
            hashtable1.Add("lastbuilddate", 9);
            hashtable1.Add("generator", 10);
            hashtable1.Add("docs", 11);
            hashtable1.Add("ttl", 12);
            <PrivateImplementationDetails>.$$method0x6000282-1 = hashtable1;
        }*/
        bool flag = false;
        bool flag2 = true;
        RssElement channel = null;
        int num = -1;
        int num2 = -1;
        if (this.reader == null)
        {
            throw new InvalidOperationException("RssReader has been closed, and can not be read.");
        }
    Label_01FA:
        flag2 = true;
        try
        {
            flag = this.reader.Read();
        }
        catch (EndOfStreamException exception)
        {
            throw new EndOfStreamException("Unable to read an RssElement. Reached the end of the stream.", exception);
        }
        catch (XmlException exception2)
        {
            if (((num != -1) || (num2 != -1)) && ((this.reader.LineNumber == num) && (this.reader.LinePosition == num2)))
            {
                throw this.exceptions.LastException;
            }
            num = this.reader.LineNumber;
            num2 = this.reader.LinePosition;
            this.exceptions.Add(exception2);
        }
        if (flag)
        {
            string str5;
            str = this.reader.Name.ToLower();
            switch (this.reader.NodeType)
            {
                case (XmlNodeType)1:
                    if (this.reader.IsEmptyElement)
                    {
                        goto Label_1404;
                    }
                    this.elementText = new StringBuilder();
                    switch (str)
                    {
                        case "item":
                            if (!this.wroteChannel)
                            {
                                this.wroteChannel = true;
                                channel = this.channel;
                                flag = false;
                            }
                            this.item = new RssItem();
                            this.channel.Items.Add(this.item);
                            goto Label_0A50;

                        case "source":
                            this.source = new RssSource();
                            this.item.Source = this.source;
                            for (int i = 0; i < this.reader.AttributeCount; i++)
                            {
                                this.reader.MoveToAttribute(i);
                                if (((str5 = this.reader.Name.ToLower()) != null) && (string.IsInterned(str5) == "url"))
                                {
                                    try
                                    {
                                        this.source.Url = new Uri(this.reader.Value);
                                    }
                                    catch (Exception exception3)
                                    {
                                        this.exceptions.Add(exception3);
                                    }
                                }
                            }
                            goto Label_0A50;

                        case "enclosure":
                            this.enclosure = new RssEnclosure();
                            this.item.Enclosure = this.enclosure;
                            for (int j = 0; j < this.reader.AttributeCount; j++)
                            {
                                this.reader.MoveToAttribute(j);
                                str5 = this.reader.Name.ToLower();
                                if (str5 != null)
                                {
                                    str5 = string.IsInterned(str5);
                                    if (str5 != "url")
                                    {
                                        if (str5 == "length")
                                        {
                                            goto Label_04CE;
                                        }
                                        if (str5 == "type")
                                        {
                                            goto Label_04FD;
                                        }
                                    }
                                    else
                                    {
                                        try
                                        {
                                            this.enclosure.Url = new Uri(this.reader.Value);
                                        }
                                        catch (Exception exception4)
                                        {
                                            this.exceptions.Add(exception4);
                                        }
                                    }
                                }
                                continue;
                            Label_04CE:
                                try
                                {
                                    this.enclosure.Length = int.Parse(this.reader.Value);
                                }
                                catch (Exception exception5)
                                {
                                    this.exceptions.Add(exception5);
                                }
                                continue;
                            Label_04FD:
                                this.enclosure.Type = this.reader.Value;
                            }
                            goto Label_0A50;

                        case "guid":
                            this.guid = new RssGuid();
                            this.item.Guid = this.guid;
                            for (int k = 0; k < this.reader.AttributeCount; k++)
                            {
                                this.reader.MoveToAttribute(k);
                                if (((str5 = this.reader.Name.ToLower()) != null) && (string.IsInterned(str5) == "ispermalink"))
                                {
                                    try
                                    {
                                        this.guid.PermaLink = bool.Parse(this.reader.Value);
                                    }
                                    catch (Exception exception6)
                                    {
                                        this.exceptions.Add(exception6);
                                    }
                                }
                            }
                            goto Label_0A50;

                        case "category":
                            this.category = new RssCategory();
                            if (((string) this.xmlNodeStack.Peek()) == "channel")
                            {
                                this.channel.Categories.Add(this.category);
                            }
                            else
                            {
                                this.item.Categories.Add(this.category);
                            }
                            for (int m = 0; m < this.reader.AttributeCount; m++)
                            {
                                this.reader.MoveToAttribute(m);
                                str5 = this.reader.Name.ToLower();
                                if (str5 != null)
                                {
                                    str5 = string.IsInterned(str5);
                                    if ((str5 == "url") || (str5 == "domain"))
                                    {
                                        this.category.Domain = this.reader.Value;
                                    }
                                }
                            }
                            goto Label_0A50;

                        case "channel":
                            this.channel = new RssChannel();
                            this.textInput = null;
                            this.image = null;
                            this.cloud = null;
                            this.source = null;
                            this.enclosure = null;
                            this.category = null;
                            this.item = null;
                            goto Label_0A50;

                        case "image":
                            this.image = new RssImage();
                            this.channel.Image = this.image;
                            goto Label_0A50;

                        case "textinput":
                            this.textInput = new RssTextInput();
                            this.channel.TextInput = this.textInput;
                            goto Label_0A50;

                        case "cloud":
                            flag2 = false;
                            this.cloud = new RssCloud();
                            this.channel.Cloud = this.cloud;
                            for (int n = 0; n < this.reader.AttributeCount; n++)
                            {
                                this.reader.MoveToAttribute(n);
                                str5 = this.reader.Name.ToLower();
                                if (str5 != null)
                                {
                                    str5 = string.IsInterned(str5);
                                    if (str5 == "domain")
                                    {
                                        this.cloud.Domain = this.reader.Value;
                                    }
                                    else if (str5 == "port")
                                    {
                                        try
                                        {
                                            this.cloud.Port = ushort.Parse(this.reader.Value);
                                        }
                                        catch (Exception exception7)
                                        {
                                            this.exceptions.Add(exception7);
                                        }
                                    }
                                    else if (str5 == "path")
                                    {
                                        this.cloud.Path = this.reader.Value;
                                    }
                                    else if (str5 == "registerprocedure")
                                    {
                                        this.cloud.RegisterProcedure = this.reader.Value;
                                    }
                                    else if (str5 == "protocol")
                                    {
                                        str5 = this.reader.Value.ToLower();
                                        if (str5 != null)
                                        {
                                            str5 = string.IsInterned(str5);
                                            if (str5 == "xml-rpc")
                                            {
                                                this.cloud.Protocol = RssCloudProtocol.XmlRpc;
                                                goto Label_08E1;
                                            }
                                            if (str5 == "soap")
                                            {
                                                this.cloud.Protocol = RssCloudProtocol.Soap;
                                                goto Label_08E1;
                                            }
                                            if (str5 == "http-post")
                                            {
                                                this.cloud.Protocol = RssCloudProtocol.HttpPost;
                                                goto Label_08E1;
                                            }
                                        }
                                        this.cloud.Protocol = RssCloudProtocol.Empty;
                                    Label_08E1:;
                                    }
                                }
                            }
                            goto Label_0A50;

                        case "rss":
                            for (int num8 = 0; num8 < this.reader.AttributeCount; num8++)
                            {
                                this.reader.MoveToAttribute(num8);
                                if (this.reader.Name.ToLower() == "version")
                                {
                                    str5 = this.reader.Value;
                                    if (str5 != null)
                                    {
                                        str5 = string.IsInterned(str5);
                                        if (str5 == "0.91")
                                        {
                                            this.rssVersion = RssVersion.RSS091;
                                            goto Label_0998;
                                        }
                                        if (str5 == "0.92")
                                        {
                                            this.rssVersion = RssVersion.RSS092;
                                            goto Label_0998;
                                        }
                                        if (str5 == "2.0")
                                        {
                                            this.rssVersion = RssVersion.RSS20;
                                            goto Label_0998;
                                        }
                                    }
                                    this.rssVersion = RssVersion.NotSupported;
                                Label_0998:;
                                }
                            }
                            goto Label_0A50;

                        case "rdf":
                            for (int num9 = 0; num9 < this.reader.AttributeCount; num9++)
                            {
                                this.reader.MoveToAttribute(num9);
                                if (this.reader.Name.ToLower() == "version")
                                {
                                    str5 = this.reader.Value;
                                    if (str5 != null)
                                    {
                                        str5 = string.IsInterned(str5);
                                        if (str5 == "0.90")
                                        {
                                            this.rssVersion = RssVersion.RSS090;
                                            goto Label_0A38;
                                        }
                                        if (str5 == "1.0")
                                        {
                                            this.rssVersion = RssVersion.RSS10;
                                            goto Label_0A38;
                                        }
                                    }
                                    this.rssVersion = RssVersion.NotSupported;
                                Label_0A38:;
                                }
                            }
                            goto Label_0A50;
                    }
                    goto Label_0A50;

                case (XmlNodeType)2:
                    goto Label_1404;

                case (XmlNodeType)3:
                    this.elementText.Append(this.reader.Value);
                    goto Label_1404;

                case (XmlNodeType)4:
                    this.elementText.Append(this.reader.Value);
                    goto Label_1404;

                case (XmlNodeType)15:
                    if (this.xmlNodeStack.Count != 1)
                    {
                        string str2 = (string) this.xmlNodeStack.Pop();
                        string str3 = (string) this.xmlNodeStack.Peek();
                        str5 = str2;
                        if (str5 != null)
                        {
                            str5 = string.IsInterned(str5);
                            if (str5 == "item")
                            {
                                channel = this.item;
                                flag = false;
                            }
                            else if (str5 == "source")
                            {
                                this.source.Name = this.elementText.ToString();
                                channel = this.source;
                                flag = false;
                            }
                            else if (str5 == "enclosure")
                            {
                                channel = this.enclosure;
                                flag = false;
                            }
                            else if (str5 == "guid")
                            {
                                this.guid.Name = this.elementText.ToString();
                                channel = this.guid;
                                flag = false;
                            }
                            else if (str5 == "category")
                            {
                                this.category.Name = this.elementText.ToString();
                                channel = this.category;
                                flag = false;
                            }
                            else if (str5 == "channel")
                            {
                                if (this.wroteChannel)
                                {
                                    this.wroteChannel = false;
                                }
                                else
                                {
                                    this.wroteChannel = true;
                                    channel = this.channel;
                                    flag = false;
                                }
                            }
                            else if (str5 == "textinput")
                            {
                                channel = this.textInput;
                                flag = false;
                            }
                            else if (str5 == "image")
                            {
                                channel = this.image;
                                flag = false;
                            }
                            else if (str5 == "cloud")
                            {
                                channel = this.cloud;
                                flag = false;
                            }
                        }
                        str5 = str3;
                        if (str5 != null)
                        {
                            str5 = string.IsInterned(str5);
                            if (str5 == "item")
                            {
                                str5 = str2;
                                if (str5 != null)
                                {
                                    str5 = string.IsInterned(str5);
                                    if (str5 == "title")
                                    {
                                        this.item.Title = this.elementText.ToString();
                                    }
                                    else if (str5 == "link")
                                    {
                                        this.item.Link = new Uri(this.elementText.ToString());
                                    }
                                    else if (str5 == "description")
                                    {
                                        this.item.Description = this.elementText.ToString();
                                    }
                                    else if (str5 == "author")
                                    {
                                        this.item.Author = this.elementText.ToString();
                                    }
                                    else if (str5 == "comments")
                                    {
                                        this.item.Comments = this.elementText.ToString();
                                    }
                                    else if (str5 == "pubdate")
                                    {
                                        try
                                        {
                                            this.item.PubDate = DateTime.Parse(this.elementText.ToString());
                                        }
                                        catch (Exception exception8)
                                        {
                                            try
                                            {
                                                string s = this.elementText.ToString();
                                                s = s.Substring(0, s.Length - 5) + "GMT";
                                                this.item.PubDate = DateTime.Parse(s);
                                            }
                                            catch
                                            {
                                                this.exceptions.Add(exception8);
                                            }
                                        }
                                    }
                                }
                            }
                            else if (str5 == "channel")
                            {
                                object obj2;
                                //GPF-1+1
                                //if (((obj2 = str2) != null) && ((obj2 = <PrivateImplementationDetails>.$$method0x6000282-1[obj2]) != null))
                                if ((obj2 = str2) != null)
                                {
                                    switch (((int) obj2))
                                    {
                                        case 0:
                                            this.channel.Title = this.elementText.ToString();
                                            goto Label_1404;

                                        case 1:
                                            try
                                            {
                                                this.channel.Link = new Uri(this.elementText.ToString());
                                            }
                                            catch (Exception exception9)
                                            {
                                                this.exceptions.Add(exception9);
                                            }
                                            goto Label_1404;

                                        case 2:
                                            this.channel.Description = this.elementText.ToString();
                                            goto Label_1404;

                                        case 3:
                                            this.channel.Language = this.elementText.ToString();
                                            goto Label_1404;

                                        case 4:
                                            this.channel.Copyright = this.elementText.ToString();
                                            goto Label_1404;

                                        case 5:
                                            this.channel.ManagingEditor = this.elementText.ToString();
                                            goto Label_1404;

                                        case 6:
                                            this.channel.WebMaster = this.elementText.ToString();
                                            goto Label_1404;

                                        case 7:
                                            this.channel.Rating = this.elementText.ToString();
                                            goto Label_1404;

                                        case 8:
                                            try
                                            {
                                                this.channel.PubDate = DateTime.Parse(this.elementText.ToString());
                                            }
                                            catch (Exception exception10)
                                            {
                                                this.exceptions.Add(exception10);
                                            }
                                            goto Label_1404;

                                        case 9:
                                            try
                                            {
                                                this.channel.LastBuildDate = DateTime.Parse(this.elementText.ToString());
                                            }
                                            catch (Exception exception11)
                                            {
                                                this.exceptions.Add(exception11);
                                            }
                                            goto Label_1404;

                                        case 10:
                                            this.channel.Generator = this.elementText.ToString();
                                            goto Label_1404;

                                        case 11:
                                            this.channel.Docs = this.elementText.ToString();
                                            goto Label_1404;

                                        case 12:
                                            try
                                            {
                                                this.channel.TimeToLive = int.Parse(this.elementText.ToString());
                                            }
                                            catch (Exception exception12)
                                            {
                                                this.exceptions.Add(exception12);
                                            }
                                            goto Label_1404;
                                    }
                                }
                            }
                            else if (str5 == "image")
                            {
                                str5 = str2;
                                if (str5 != null)
                                {
                                    str5 = string.IsInterned(str5);
                                    if (str5 == "url")
                                    {
                                        try
                                        {
                                            this.image.Url = new Uri(this.elementText.ToString());
                                        }
                                        catch (Exception exception13)
                                        {
                                            this.exceptions.Add(exception13);
                                        }
                                    }
                                    else if (str5 == "title")
                                    {
                                        this.image.Title = this.elementText.ToString();
                                    }
                                    else if (str5 == "link")
                                    {
                                        try
                                        {
                                            this.image.Link = new Uri(this.elementText.ToString());
                                        }
                                        catch (Exception exception14)
                                        {
                                            this.exceptions.Add(exception14);
                                        }
                                    }
                                    else if (str5 == "description")
                                    {
                                        this.image.Description = this.elementText.ToString();
                                    }
                                    else if (str5 == "width")
                                    {
                                        try
                                        {
                                            this.image.Width = byte.Parse(this.elementText.ToString());
                                        }
                                        catch (Exception exception15)
                                        {
                                            this.exceptions.Add(exception15);
                                        }
                                    }
                                    else if (str5 == "height")
                                    {
                                        try
                                        {
                                            this.image.Height = byte.Parse(this.elementText.ToString());
                                        }
                                        catch (Exception exception16)
                                        {
                                            this.exceptions.Add(exception16);
                                        }
                                    }
                                }
                            }
                            else if (str5 == "textinput")
                            {
                                str5 = str2;
                                if (str5 != null)
                                {
                                    str5 = string.IsInterned(str5);
                                    if (str5 == "title")
                                    {
                                        this.textInput.Title = this.elementText.ToString();
                                    }
                                    else if (str5 == "description")
                                    {
                                        this.textInput.Description = this.elementText.ToString();
                                    }
                                    else if (str5 == "name")
                                    {
                                        this.textInput.Name = this.elementText.ToString();
                                    }
                                    else if (str5 == "link")
                                    {
                                        try
                                        {
                                            this.textInput.Link = new Uri(this.elementText.ToString());
                                        }
                                        catch (Exception exception17)
                                        {
                                            this.exceptions.Add(exception17);
                                        }
                                    }
                                }
                            }
                            else if (str5 == "skipdays")
                            {
                                if (str2 == "day")
                                {
                                    switch (this.elementText.ToString().ToLower())
                                    {
                                        case "monday":
                                            this.channel.SkipDays[0] = true;
                                            goto Label_1404;

                                        case "tuesday":
                                            this.channel.SkipDays[1] = true;
                                            goto Label_1404;

                                        case "wednesday":
                                            this.channel.SkipDays[2] = true;
                                            goto Label_1404;

                                        case "thursday":
                                            this.channel.SkipDays[3] = true;
                                            goto Label_1404;

                                        case "friday":
                                            this.channel.SkipDays[4] = true;
                                            goto Label_1404;

                                        case "saturday":
                                            this.channel.SkipDays[5] = true;
                                            break;

                                        case "sunday":
                                            this.channel.SkipDays[6] = true;
                                            break;
                                    }
                                }
                            }
                            else if ((str5 == "skiphours") && (str2 == "hour"))
                            {
                                this.channel.SkipHours[byte.Parse(this.elementText.ToString().ToLower())] = true;
                            }
                        }
                    }
                    goto Label_1404;
            }
        }
        goto Label_1404;
    Label_0A50:
        if (flag2)
        {
            this.xmlNodeStack.Push(str);
        }
    Label_1404:
        if (flag)
        {
            goto Label_01FA;
        }
        return channel;
    }

    // Properties
    public ExceptionCollection Exceptions
    {
        get
        {
            return this.exceptions;
        }
    }

    public RssVersion Version
    {
        get
        {
            return this.rssVersion;
        }
    }
}
}
